// pages/search/search.js
const app = getApp()
var service = require('../../util/service')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fjsjs:[],
    lisss:[],
    bmgzs:[],
    searchValue:"",
    isss:false,
    lat:"",
    lon:"",
    orgList:[],
    lodding: false,
    loddtext: "加载中……",
    pageSize: 10,
    page: 1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var fjsjs = ["宝宝成长园"]
    var lisss = wx.getStorageSync('lisss');
    var bmgzs = ["智力发育", "安全教育", "早教", "英孚英语少儿英语", "宝宝叛逆", "宝宝内向"]
      this.setData({
        'fjsjs': fjsjs,
        lisss: lisss,
        bmgzs: bmgzs
      })

    
  },
  bindSarchInput:function(e){
    this.setData({
      searchValue: e.detail.value,
      isss: e.detail.value!=""
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getDistance: function (lat1, lng1, lat2, lng2) {
    lat1 = lat1 || 0;
    lng1 = lng1 || 0;
    lat2 = lat2 || 0;
    lng2 = lng2 || 0;
    var rad1 = lat1 * Math.PI / 180.0;
    var rad2 = lat2 * Math.PI / 180.0;
    var a = rad1 - rad2;
    var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
    var r = 6378137;
    return ((r * 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(rad1) * Math.cos(rad2) * Math.pow(Math.sin(b / 2), 2)))) / 1000).toFixed(1)
  },
  setOrgList: function () {
    var that = this;
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        that.setData({
          lat: res.latitude,
          lon: res.longitude,
        });
        that.getProvs();
      }
    });
  },
  lissstop:function(e){
    this.setData({
      searchValue: e.target.dataset.code
    })
    this.setOrgList();
  },
  clearsearch:function(){
    var that = this;
    if (this.data.isss){
      var lisss = this.data.lisss||[];
      if (lisss.indexOf(that.data.searchValue) < 0){
        lisss.unshift(that.data.searchValue);
        if (lisss.length>10){
          lisss = lisss.slice(0, 10);
        }
        that.setData({
          lisss: lisss
        });
        wx.setStorageSync('lisss', lisss);
      }
      this.setOrgList();
    }else{
      wx.navigateBack();
    }
  },
  setOrgList: function () {
    var that = this;
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        that.setData({
          lat: res.latitude,
          lon: res.longitude,
          'page': 1,
          orgList: []
        });
        that.getProvs();
      }
    });
  },
  lower: function () {
    this.setData({
      'page': this.data.page + 1
    })
    this.getProvs();
  },
  getProvs: function () {
    var that = this;
    that.setData({
      lodding: true,
      loddtext: "加载中……"
    })
    service.AJAX("smc/base/SystemProviderUser/querySystemProviderUser.json", { page: that.data.page, pageSize: that.data.pageSize, x: that.data.lat, y: that.data.lon, "$prvName-contains": that.data.searchValue}, res => {
      var orgList = res.rows;
      var lodd = orgList.length < 10;
      console.log(orgList);
      for (var i = 0; i < orgList.length; i++) {
        var price = orgList[i].minPrice + "";
        var activitys = orgList[i].prodLabel;
        if (activitys && activitys != "") {
          orgList[i].activity = activitys.split(/[；;]/);
        }
        orgList[i].attribute1 = service.imgurl + orgList[i].attribute1;
        orgList[i].minPrice = price.indexOf(".") > 0 ? price.split(".") : (price + ".00").split(".");
        orgList[i].distance = that.getDistance(that.data.lat, that.data.lon, orgList[i].posX, orgList[i].posY);
      }
      var data = that.data.orgList.concat(orgList);
      that.setData({
        orgList: data,
        lodding: lodd,
        loddtext: lodd ? "没有更多数据了" : "加载中……"
      })
    }, err => { }, 'POST')
  }
})